﻿Imports BUS, DTO
Public Class frmTraSach
    Dim dsTra As New List(Of TraSach_ChiTietTraSachDTO)
    Dim dsMuon As New List(Of MuonSach_ChiTietMuonSachDTO)
    Dim dsChiMuon As New List(Of MuonSach_ChiTietMuonSachDTO)
    Dim dsDocGia As New List(Of DocGiaDTO)
    Dim thamso As New ThamSoDTO
    Dim tongnoDocGia As Integer = 0
    Dim tongtienphat As Integer = 0
    Dim tongnosauKhiPhat As Integer = 0


    Private Sub frmTraSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        

        Dim docgiaBus As New DocGiaBUS()
        dsDocGia = docgiaBus.LayDSDocGia()

        _LookUpEdit_docgia.Properties.DataSource = dsDocGia
        _LookUpEdit_docgia.Properties.ValueMember = "MaDocGia"
        _LookUpEdit_docgia.Properties.DisplayMember = "TenDocGia"

        Dim tsBus As New ThamSoBUS
        thamso = tsBus.LayThongtin()
    End Sub

    Private Sub _LookUpEdit_docgia_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _LookUpEdit_docgia.EditValueChanged
        tongnoDocGia = 0
        tongnosauKhiPhat = 0
        tongtienphat = 0
        _TextEdit_No.Text = "0"
        _TextEdit_Phat.Text = "0"
        _TextEdit_TongNo.Text = "0"
        _TextEdit_TienDocGiaTra.Text = "0"


        Dim muonSachBus As New MuonSach_ChiTietMuonSachBUS()
        dsChiMuon.Clear()
        dsMuon.Clear()
        dsTra.Clear()


        dsMuon = muonSachBus.LayDSPMS_CTPMSTheoMaDocGia(_LookUpEdit_docgia.EditValue)

        Dim traSachBus As New TraSach_ChiTietTraSachBUS()
        dsTra = traSachBus.LayDSPTS_CTPTSTheoMaDocGia(_LookUpEdit_docgia.EditValue)

        Dim docgiaBus As New DocGiaBUS
        Dim docgiaDto As New DocGiaDTO
        docgiaDto = docgiaBus.LayThongTinDocGiaBangMaDocGia(_LookUpEdit_docgia.EditValue)

        _TextBox_NgaySinh.Text = docgiaDto.NgaySinh.ToString()
        _TextBox_DiaChi.Text = docgiaDto.DiaChi


        _DataGridView_Muon.Rows.Clear()
        _DataGridView_Tra.Rows.Clear()
        Dim flag As Integer = 0

        For index As Integer = 0 To dsMuon.Count - 1
            For index1 As Integer = 0 To dsTra.Count - 1
                If (dsMuon(index).MaSach = dsTra(index1).MaSach And dsMuon(index).NgayMuon <= dsTra(index1).NgayTra) Then
                    flag = 1
                End If
            Next

            If flag = 0 Then
                dsChiMuon.Add(dsMuon(index))
            End If

            flag = 0
        Next

        For Each item As MuonSach_ChiTietMuonSachDTO In dsChiMuon
            Dim sachBus As New SachBUS()
            Dim sachDto As New SachDTO()
            sachDto = sachBus.LayThongTinSachTheoMaSach(item.MaSach)

            Dim tacgiaBUs As New TacGiaBUS()
            Dim tacgiaDto As New TacGiaDTO
            tacgiaDto = tacgiaBUs.LayThongTinTacGiaTheoMa(item.MaTacGia)

            Dim theloaiBus As New LoaiSachBUS
            Dim loaisachDto As New LoaiSachDTO
            loaisachDto = theloaiBus.LayThongTinTheLoaiTheoMa(item.MaLoaiSach)

            _DataGridView_Muon.Rows.Add(sachDto.MaSach, sachDto.TenSach, loaisachDto.TenTheLoai, tacgiaDto.TenTacGia, item.NgayMuon)

        Next

        Dim ptBus As New PhieuTraSachBUS
        tongnoDocGia = ptBus.LayTongNoTheoMaDocGia(_LookUpEdit_docgia.EditValue)


    End Sub

    Private Sub _SimpleButton_Tra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _SimpleButton_Tra.Click
        

        Dim songaymuon As Integer = (DateTime.Now - DateTime.Parse(_DataGridView_Muon.CurrentRow.Cells(4).Value)).Days
        Dim songaytratre As Integer = songaymuon - thamso.SoNgayMuonSachToiDa

        If songaytratre <= 0 Then
            songaytratre = 0
        End If


        _DataGridView_Tra.Rows.Add(_DataGridView_Muon.CurrentRow.Cells(0).Value, _DataGridView_Muon.CurrentRow.Cells(1).Value, _DataGridView_Muon.CurrentRow.Cells(2).Value, _DataGridView_Muon.CurrentRow.Cells(3).Value, songaymuon, songaytratre, songaytratre * thamso.TienPhat)
        _DataGridView_Muon.Rows.Remove(_DataGridView_Muon.CurrentRow)

        _TextEdit_No.Text = tongnoDocGia.ToString()



        For index As Integer = 0 To _DataGridView_Tra.Rows.Count - 2
            tongtienphat += Integer.Parse(_DataGridView_Tra.Item(6, index).Value)
        Next

        _TextEdit_Phat.Text = tongtienphat.ToString()

        tongnosauKhiPhat = (tongnoDocGia + tongtienphat)
        _TextEdit_TongNo.Text = tongnosauKhiPhat.ToString()

    End Sub

    Private Sub _SimpleButton_Xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _SimpleButton_Xoa.Click
        For Each item As MuonSach_ChiTietMuonSachDTO In dsChiMuon
            If item.MaSach = _DataGridView_Tra.CurrentRow.Cells(0).Value Then
                _DataGridView_Muon.Rows.Add(_DataGridView_Tra.CurrentRow.Cells(0).Value, _DataGridView_Tra.CurrentRow.Cells(1).Value, _DataGridView_Tra.CurrentRow.Cells(2).Value, _DataGridView_Tra.CurrentRow.Cells(3).Value, item.NgayMuon)
                _DataGridView_Tra.Rows.Remove(_DataGridView_Tra.CurrentRow)
                Exit For
            End If
        Next
    End Sub

    Private Sub _SimpleButton_Nhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _SimpleButton_Nhap.Click

        Dim dsPhieuTraSach As New List(Of PhieuTraSachDTO)
        Dim phieuTraSachBus As New PhieuTraSachBUS
        dsPhieuTraSach = phieuTraSachBus.LayDSPhieuTra()

        Dim strNgayTra As String = String.Format("{0:dd/MM/yyyy}", _DateTimePicker_NgayTra.Value)
        MessageBox.Show(strNgayTra)
        Dim sql As String = "insert into PHIEUTRASACH (MaDocGia,NgayTra,TienPhatKyNay,TienNo,TongNo,MaChiTietPhieuTraSach) Values ( "
        sql = sql & _LookUpEdit_docgia.EditValue.ToString() & " , '" & strNgayTra & "', " & _TextEdit_Phat.Text & ", " & _TextEdit_No.Text & " , "
        sql = sql & (Integer.Parse(_TextEdit_TienDocGiaTra.Text) - tongnosauKhiPhat).ToString() & ", " & (dsPhieuTraSach.Count + 1).ToString() & ")"
        'MessageBox.Show(sql)

        Dim kq1 As Integer = phieuTraSachBus.ThemPhieuTra(sql)

        If kq1 <> 1 Then
            MessageBox.Show("Thêm phiếu trả sách thất bại !")
            Return
        End If



        For index As Integer = 0 To _DataGridView_Tra.Rows.Count - 1
            For Each item As MuonSach_ChiTietMuonSachDTO In dsChiMuon
                If item.MaSach = _DataGridView_Tra.Item(0, index).Value Then
                    sql = "insert into CHITIETPHIEUTRASACH (MaChiTietPhieuTraSach,MaSach,NgayMuon,SoNgayMuon,TienPhat,MaTheLoai,MaTacGia) Values "
                    sql = sql & " ( " & (dsPhieuTraSach.Count + 1).ToString() & ", " & _DataGridView_Tra.Item(0, index).Value.ToString() & ",'" & item.NgayMuon & "', "
                    sql = sql & _DataGridView_Tra.Item(4, index).Value.ToString() & ", " & _DataGridView_Tra.Item(6, index).Value.ToString() & ", "
                    sql = sql & item.MaLoaiSach & ", " & item.MaTacGia & ")"
                    'MessageBox.Show(sql)
                    Dim chitietPhieuTraSachBus As New ChiTietPhieuTraSachBUS
                    kq1 = chitietPhieuTraSachBus.ThemChiTietPhieuTraSach(sql)

                    If kq1 <> 1 Then
                        MessageBox.Show("Thêm chi tiết phiếu trả sách thất bại !")
                        Return
                    End If
                End If
            Next
        Next



    End Sub

    Private Sub _TextEdit_TienDocGiaTra_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _TextEdit_TienDocGiaTra.EditValueChanged

        Try
            If Integer.Parse(_TextEdit_TienDocGiaTra.Text) > tongnosauKhiPhat Then
                MessageBox.Show("Số tiền nhâp ko vượt quá số tiền độc giả nợ !")
                _TextEdit_TienDocGiaTra.Text = "0"
            End If
        Catch ex As Exception
            MessageBox.Show("Yêu cầu nhập đúng kiểu dữ liệu !")
        End Try


    End Sub
End Class